local skynet = require "skynet"
local socket = require "socket"

local parser = require "sprotoparser"
local sproto = require "sproto"
local core = require "sproto.core"
local print_r = require "print_r"

--配置
local ip = "127.0.0.1"
local port = 8001

local protoString = [[
.Person {
    name 0 : string
    id 1 : integer
    email 2 : string
}
]]

local function echo(id)
	socket.start(id)
	while true do
		local str = socket.read(id)
		if str then
			print("--------send form client start----------")
			print(str)
			print("--------send form client end----------")
			local response = ""
			
			--socket.write(id, response)
		else
			socket.close(id)
			print("connection closed!")
			return
		end
	end
end

skynet.start(function ( ... )
	local id = socket.listen(ip, port)
	print("gameserver启动地址+端口:", "127.0.0.1", 8001)

	local proto = sproto.parse(protoString)
	local def = proto:default "Person"
	print("default table for Person")
	print_r(def)
	print("--------------")

	--core.dumpproto(proto.__cobj)

	socket.start(id, function (id, addr)
		print("connect from " .. addr .. " " .. id)

		skynet.fork(echo, id)
		print("fork 立即返回")
	end)

	print("start 立即返回")
end)